package com.zzb.service.impl;

import com.zzb.DTO.CommentDTO;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.CommentVO;
import com.zzb.dao.CommentDao;
import com.zzb.dao.impl.CommentDaoImpl;
import com.zzb.entity.CommentEntity;
import com.zzb.service.CommentService;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class CommentServiceImpl implements CommentService {
    CommentEntity commentEntity = new CommentEntity();
    CommentDao commentDao = new CommentDaoImpl();

    @Override
    public ResultDTO add(CommentVO commentVO) {
        return null;
    }

    @Override
    public ResultDTO delete(CommentVO commentVO) {
        //数据复制
        BeanUtils.copyProperties(commentVO, commentEntity);
        //判断产品是否存在
        if (commentDao.findById(commentEntity) != null) {//存在
            return ResultDTO.getResult("success", commentDao.delete(commentEntity));
        } else {//不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(CommentVO commentVO) {
        BeanUtils.copyProperties(commentVO, commentEntity);
        commentEntity = commentDao.findById(commentEntity);
        if (commentEntity != null) {
            CommentDTO commentDTO = new CommentDTO();
            BeanUtils.copyProperties(commentEntity, commentDTO);
            return ResultDTO.getResult("success", commentDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findAll(CommentVO commentVO) {
        BeanUtils.copyProperties(commentVO,commentEntity);
        int count = commentDao.count(commentEntity);
        List<CommentEntity> list = commentDao.findAll(commentEntity);
        //集合复制
        List<CommentDTO> commentDTOList = Tool.copy(list, CommentDTO.class);
        return ResultDTO.getResult(count, commentDTOList);
    }

    @Override
    public ResultDTO update(CommentVO commentVO) {
        return null;
    }
}
